<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>

    <form:form commandName="map" cssClass="form-horizontal" role="form" onsubmit="return false;">
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="taskName">
            <a style="color: red">*</a>任务名称
        </label>
        <div class="col-sm-8">
            <input type="text" name="taskName" id="taskName" readonly placeholder="任务名称" value="${taskNew.taskName}"
                   class="form-control required">
        </div>
    </div>
    <div class="form-group">
        <c:if test="${!empty map.taskSend.remark}">
            <label class="col-sm-2 control-label no-padding-right">议题</label>
            <div class="col-sm-3">
                <input type="text" value="${taskSend.remark}" class="form-control" readonly>
            </div>
        </c:if>
        <label class="col-sm-2 control-label no-padding-right">承办单位</label>
        <div class="col-sm-3">
            <input type="text" value="${taskSend.departName}" class="form-control" readonly>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="content"><span style="color: red">*</span>申请内容</label>
        <div class="col-sm-8">
            <textarea rows="3" id="content" name="content" class="form-control" required></textarea>
        </div>
    </div>



    <%--议题或承办单位内容DIV--%>
    <div id="departContent"></div>
    <input type="hidden" name="sendId" id="sendId" value="${taskSend.sysId}">

    <%--  <form:hidden path="${map.message.sysId}"/>--%>

</form:form>
<script src="<s:url value="/assets/js/chosen.jquery.js"></s:url>"></script>
<script src="<s:url value="/assets/js/ajaxfileupload.js"></s:url>"></script>
<script src="<s:url value="/assets/js/modal/modal.js"></s:url>"></script>

<script type="text/javascript">
    jQuery(function ($) {
        $('.date-picker').datepicker({
            inline: true,
            format: "yyyy-mm-dd",
            autoclose: true,
            todayHighlight: true,
            language: 'cn'
        })
        //show datepicker when clicking on the icon
            .next().on(ace.click_event, function () {
            $(this).prev().focus();
        });

        // 获取承办单位
        getDepartCategory();

        // 获取督查人信息
        getOperator();
    });

    function dele() {
        var result = confirm('确认删除吗?');
        if (result == true) {
            console.log();
            $.ajax({
                url: '/message/deleteSend',
                type: "GET",
                data: "id=" + $("#delete").val(),
                dataType: 'json',
                success: function (res) {
                    res
                    alert("删除成功");
                }
            })
        }
        if (result == false) {
            alert("未删除")
        }

    }

    // 督查人手机号Map
    var mobileMap = new Map();

    /**
     * 获取督查人信息
     */
    function getOperator() {
        $.ajax({
            type: "post",
            url: '/project/queryByDeptId',
            dataType: "json",
            success: function (data) {
                if (data == undefined || data == null) {
                    console.log("获取督查人信息为空");
                    return;
                }
                var operatorOption = '';
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].name;
                    var sysId = data[i].sysId;
                    var mobile = data[i].mobile;
                    operatorOption += '<option value="' + sysId + '">' + name + '</option>';
                    mobileMap.set(sysId, mobile);
                }
                // 这么写是因为在查询页也有一个相同的id,查询页是隐藏的
                $('#taskNew #operatorId').append(operatorOption);
            },
            error: function (jqXHR) {
                console.log("获取督查人信息失败");
            }
        });
    }

    /**
     * 回填督查人电话
     */
    function changeOperator(obj) {
        var operatorName = $(obj).find("option:selected").text();
        if (operatorName == "点击选择") {
            $(obj).siblings("input").val('');
            $('#taskNew #operatorTel').val('');
        } else {
            $(obj).siblings("input").val(operatorName);
            var operatorId = $(obj).val();
            var mobile = mobileMap.get(operatorId);
            $('#taskNew #operatorTel').val(mobile);
        }
    }

    // 承办单位分类option字符串
    var departCategoryOption = '<option value="">点击选择</option>';

    /**
     * 获取单位分类
     */
    // function getDepartCategory() {
    //     $.ajax({
    //         type: "post",
    //         url: getUrl("/deptList"),
    //         dataType: "json",
    //         success: function (data) {
    //             if (data == undefined || data == null) {
    //                 console.log("获取承办单位为空");
    //                 return;
    //             }
    //             for (var i = 0; i < data.length; i++) {
    //                 var deptName = data[i].deptName;
    //                 var deptRelation = data[i].deptRelation;
    //                 departCategoryOption += '<option value="' + deptRelation + '">' + deptName + '</option>';
    //             }
    //         },
    //         error: function (jqXHR) {
    //             console.log("获取承办单位失败");
    //         }
    //     });
    // }

    // 承办单位的index
    var departIndex = $("#departContent .form-group").length;

    /**
     * 修改事项类型
     */
    function changeCategory(obj) {
        // 下拉框选中的text
        var text = $(obj).find('option:selected').text();
        $('#departContent').empty();
        if (text == null || text == '点击选择') {
            $("#addDepart").css("display", "none");
            return;
        }
        $("#addDepart").css("display", "block");
        // 如果类型是会议纪要,文号必填
        if (text != null && text == '会议纪要') {
            $('#taskNew #referenceNumber').addClass('required');
        } else {
            $('#taskNew #referenceNumber').removeClass('required');
        }
        addDeparts(text);
    }

    /**
     * 增加议点和承办单位
     */
    function addDeparts(text) {
        if (text == null || text == 'undefined' || text == '') {
            text = $('#taskNew #categoryId').find('option:selected').text();
        }
        var remarkStr = 'list[' + departIndex + '].remark';
        var deptStr = 'list[' + departIndex + '].departName';
        var userIdStr = 'list[' + departIndex + '].userId';
        var html;

        if (text != null && text == '01') {
            html = '<div class="form-group">\n' +
                '    <label class="col-sm-2 control-label no-padding-right" for="' + remarkStr + '"><a style="color: red">*</a>议题</label>\n' +
                '    <div class="col-sm-2">\n' +
                '        <input type="text" name="' + remarkStr + '" id="' + remarkStr + '" placeholder="议题" \n' +
                '               class="form-control required" />\n' +
                '    </div>\n' +
                '    <label class="col-sm-1 control-label no-padding-right"><a style="color: red">*</a>承办单位</label>\n' +
                '    <div class="col-sm-2">\n' +
                '        <select class="form-control required" onchange="getChildDepart(this)">\n' + departCategoryOption +
                '        </select>\n' +
                '    </div>\n' +
                '    <div class="col-sm-3">\n' +
                '        <select class="form-control required" name="' + userIdStr + '" id="' + userIdStr + '" onchange="changeDepart(this)">\n' +
                '           <option value="">点击选择</option>\n' +
                '        </select>\n' +
                '        <input type="hidden" name="' + deptStr + '" id="' + deptStr + '" />\n' +
                '    </div>\n' +
                '    <div class="col-sm-1">\n' +
                '        <input type="button" value="删除" class="btn-sm btn-danger" />\n' +
                '    </div>\n' +
                '</div>';
        } else {
            html = '<div class="form-group">\n' +
                '    <label class="col-sm-2 control-label no-padding-right"><a style="color: red">*</a>承办单位</label>\n' +
                '    <div class="col-sm-2">\n' +
                '        <select class="form-control required" onchange="getChildDepart(this)">\n' + departCategoryOption +
                '        </select>\n' +
                '    </div>\n' +
                '    <div class="col-sm-3">\n' +
                '        <select name="' + userIdStr + '" id="' + userIdStr + '" class="form-control required" onchange="changeDepart(this)">\n' +
                '           <option value="">点击选择</option>\n' +
                '        </select>\n' +
                '        <input type="hidden" name="' + deptStr + '" id="' + deptStr + '" />\n' +
                '    </div>\n' +
                '    <div class="col-sm-1">\n' +
                '        <input type="button" value="删除" class="btn-sm btn-danger" />\n' +
                '    </div>\n' +
                '</div>';
        }
        $('#departContent').append(html);
        departIndex++;
    }

    /**
     * 获取单位分类下的所有单位
     */
    function getChildDepart(obj) {
        var deptRelation = $(obj).val();
        $.ajax({
            type: "post",
            url: getUrl("childDeptList"),
            data: {
                deptRelation: deptRelation
            },
            dataType: "json",
            success: function (data) {
                if (data == undefined || data == null) {
                    console.log("获取单位分类下的所有单位为空");
                    return;
                }
                // 选到具体单位的下拉框节点
                var childDepartSelect = $(obj).parent().next().children(".form-control");
                // 清空下拉框
                childDepartSelect.empty();
                var childDepartOption = '<option value="">点击选择</option>';
                for (var i = 0; i < data.length; i++) {
                    var deptName = data[i].deptName;
                    var userId = data[i].userId;
                    childDepartOption += '<option value="' + userId + '">' + deptName + '</option>';
                }
                // 将数据添加到具体单位的下拉框
                childDepartSelect.append(childDepartOption);
            },
            error: function (jqXHR) {
                console.log("获取单位分类下的所有单位失败");
            }
        });
    }

    /**
     * 修改承办单位,给隐藏框赋值
     */
    function changeDepart(obj) {
        var departName = $(obj).find("option:selected").text();
        if (departName == "点击选择") {
            $(obj).siblings("input").val('');
        } else {
            $(obj).siblings("input").val(departName);
        }
    }

    /**
     * 删除议题和承办单位
     */
    $("#departContent").on("click", ".btn-danger", function () {
        $(this).parent().parent().remove();
    });

    /**
     * 上传附件
     */
    $("#btn-upload").click(function () {
        var fileVal = $("#fileupload").val();
        if (fileVal == null || fileVal == "") {
            alert("未选择任何文件");
            return;
        }
        // 判断文件大小是否大于10M
        var fileSize = $("#fileupload")[0].files[0].size / 1024 / 1024;
        if (fileSize > 10) {
            alert("上传文件大小不能大于10M");
            return;
        }
        $(".shade").fadeIn(100);
        $(".warning").fadeIn(100);
        $('.la-square-jelly-box').show();
        showShade();
        //警告框自定义内容
        $('.warning>.title>em').html('加载中……');
        $.ajaxFileUpload({
            type: "post",
            fileElementId: "fileupload",
            secureuri: false,
            dataType: 'text',
            url: "/filesUpload/uploadBatch",
            // data:fileinfo,
            async: false,
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data);
                var val = data.data.sysid;
                $("#file").attr("value", val);
                if (data.code === "200") {
                    $('.warning>.title>em').html('上传成功');
                } else {
                    $('.warning>.title>em').html('上传失败');
                }
            },
            error: function (data) {
                alert("附件上传失败 ！！请联系管理员");
            }
        });
    });
</script>
